<template>
	<view class="content">
		<view class="header">
			<uv-button v-show="done" type="primary" text="当日已报产" @click="() => (done = !done)"></uv-button>
			<uv-button v-show="!done" type="primary" text="待报产作业" @click="() => (done = !done)"></uv-button>
			<uv-button v-show="done" type="warning" text="报产" @click="goReportProductionQuery"></uv-button>
		</view>

		<view class="flex">
			<uv-input v-model="lotNo" placeholder="请输入指令"></uv-input>
			<uv-input @tap="open" v-model="dateStart" placeholder="缴库开始日期" disabled disabledColor="#ffffff"></uv-input>
			<uv-icon name="more-dot-fill"></uv-icon>
			<uv-input @tap="open2" v-model="dateEnd" placeholder="缴库结束日期" disabled disabledColor="#ffffff"></uv-input>
		</view>
		<view class="flex AIC" style="margin-top: 5px;align-items: center;">
			<span>报账日期：</span>
			<uv-input @tap="open3" v-model="sysDateStart" placeholder="报账开始日期" disabled
				disabledColor="#ffffff"></uv-input>
			<uv-icon name="more-dot-fill"></uv-icon>
			<uv-input @tap="open4" v-model="sysDateEnd" placeholder="报账结束日期" disabled
				disabledColor="#ffffff"></uv-input>
		</view>
		<view class="search">
			<button @click="search" class="button">搜索</button>
			<button @click="reset" class="buttonRS">重置</button>
		</view>

		<uni-table v-if="done" border stripe emptyText="暂无更多数据">
			<!-- 表头行 -->
			<uni-tr>
				<uni-th :width="60" align="center">组别</uni-th>
				<uni-th :width="50" align="center">指令</uni-th>
				<uni-th :width="50" align="center">类型</uni-th>
				<uni-th :width="60" align="center">缴库日期</uni-th>
				<uni-th :width="60" align="center">缴库数量</uni-th>
			</uni-tr>
			<!-- 表格数据行 -->
			<uni-tr v-for="item in tableList" @tap="lookDone(item)">
				<uni-td>{{ item.deptName }}</uni-td>
				<uni-td>{{ item.lotNo }}</uni-td>
				<uni-td>{{ item.orderType2 }}</uni-td>
				<uni-td>{{ item.slipDate }}</uni-td>
				<uni-td>{{ item.qty }}</uni-td>
			</uni-tr>
		</uni-table>
		<!-- 分页 -->
		<uni-pagination v-if="done" :total="total" :current="current" title="标题文字" prev-text="前一页" next-text="后一页" />

		<uni-table v-if="!done" border stripe emptyText="暂无更多数据">
			<!-- 表头行 -->
			<uni-tr>
				<uni-th :width="50" align="center">组别</uni-th>
				<uni-th :width="50" align="center">指令</uni-th>
				<uni-th :width="50" align="center">类型</uni-th>
				<uni-th :width="60" align="center">缴库日期</uni-th>
				<uni-th :width="60" align="center">缴库数量</uni-th>
			</uni-tr>
			<!-- 表格数据行 -->
			<uni-tr v-for="item in tableList2">
				<uni-td>{{ item.deptName }}</uni-td>
				<uni-td>{{ item.lotNo }}</uni-td>
				<uni-td>{{ item.orderType2 }}</uni-td>
				<uni-td>{{ item.slipDate }}</uni-td>
				<uni-td>{{ item.qty }}</uni-td>
			</uni-tr>
		</uni-table>

		<uv-tabbar iconSize="30" :border="false" :value="value" @change="tabbarChange">
			<uv-tabbar-item text="报产作业" icon="order"></uv-tabbar-item>
			<uv-tabbar-item text="产值查询" icon="search"></uv-tabbar-item>
			<uv-tabbar-item text="退出" icon="play-right"></uv-tabbar-item>
		</uv-tabbar>

		<uv-notify ref="notify" type="error"></uv-notify>

		<uv-datetime-picker :formatter="formatter" v-model="dateValue" ref="datetimePicker" mode="date"
			@confirm="confirm"></uv-datetime-picker>
		<uv-datetime-picker :formatter="formatter" v-model="dateValue2" ref="datetimePicker2" mode="date"
			@confirm="confirm2"></uv-datetime-picker>

		<uv-datetime-picker :formatter="formatter" v-model="dateValue3" ref="datetimePicker3" mode="date"
			@confirm="confirm3"></uv-datetime-picker>
		<uv-datetime-picker :formatter="formatter" v-model="dateValue4" ref="datetimePicker4" mode="date"
			@confirm="confirm4"></uv-datetime-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				current: 1,
				total: 0,
				dateValue: Number(new Date()),
				dateValue2: Number(new Date()),
				dateValue3: Number(new Date()),
				dateValue4: Number(new Date()),
				sysDateStart: this.formatDate(new Date()),
				sysDateEnd: "",
				lotNo: "",
				dateStart: "",
				dateEnd: "",
				tabbarValue: "",
				done: true,
				title: "Hello",
				value: 0,
				searchVal: "",
				tableList: [],
				tableList2: [],
				userInfo: {},
				searchObj: {}
			};
		},
		onLoad() {
			this.selectedIndexs = [];
		},
		methods: {
			open() {
				this.$refs.datetimePicker.open();
			},
			confirm(e) {
				this.dateStart = this.formatDate(e.value);
			},
			open2() {
				this.$refs.datetimePicker2.open();
			},
			confirm2(e) {
				this.dateStart = this.formatDate(e.value);
			},
			open3() {
				this.$refs.datetimePicker3.open();
			},
			confirm3(e) {
				this.sysDateStart = this.formatDate(e.value);
			},
			open4() {
				this.$refs.datetimePicker4.open();
			},
			confirm4(e) {
				this.sysDateEnd = this.formatDate(e.value);
			},
			// 分页
			change(e) {
				console.log(e)
				this.current = e.current
			},
			formatDate(timestamp) {
				let date = new Date(timestamp);
				let year = date.getFullYear();
				let month = "0" + (date.getMonth() + 1); // getMonth()返回的月份是从0开始的，这里添加前导零
				let day = "0" + date.getDate(); // 添加前导零
				return year + "-" + month.substr(-2) + "-" + day.substr(-2); // substr(-2)确保至少取两位数字，避免月份或日期是个位数时的问题
			},
			reset() {
				this.lotNo = "";
				this.dateStart = "";
				this.dateEnd = "";
				this.sysDateStart = "";
				this.sysDateEnd = ""
			},
			search() {
				this.getTableList();
				this.getTableList2();
			},
			goReportProductionQuery() {
				// 取消查询直接到报产
				this.userInfo = uni.getStorageSync("userInfo");
				uni.navigateTo({
					url: "/pages/reportProductSearch/reportProductSearch?deptName=" + this.userInfo.departmentList[
							0].deptName + "&deptNo=" + this.userInfo.departmentList[0].deptNo + "&lotNo=" + "" +
						"&dateStart1=" + "" + "&dateEnd1=" + "",
				});

				// uni.navigateTo({
				//   url: "/pages/reportProductionQuery/reportProductionQuery",
				// });
			},
			tabbarChange(index) {
				this.tabbarValue = index;
				if (index === 1) {
					uni.reLaunch({
						url: "/pages/outputSearch/outputSearch",
					});
				}
				if (index === 2) {
					uni.reLaunch({
						url: "/pages/login/login",
					});
				}
			},
			getTableList() {
				let token = uni.getStorageSync("token");
				let userInfo = uni.getStorageSync("userInfo");
				const now = new Date();
				const year = now.getFullYear();
				const month = (now.getMonth() + 1).toString().padStart(2, "0");
				const day = now.getDate();
				const dateBase = year + "-" + month + "-" + day;
				uni.request({
					url: "/kl/pad/stitchProductionReport/getReportProductDetails",
					header: {
						Authorization: token, //自定义请求头信息
					},
					data: {
						...userInfo,
						deptName: userInfo.departmentList[0].deptName,
						deptNo: userInfo.departmentList[0].deptNo,
						sysDate: dateBase,
						lotNo: this.lotNo,
						dateStart1: this.dateStart,
						dateEnd1: this.dateEnd,
						sysDateStart: this.sysDateStart,
						sysDateEnd: this.sysDateEnd,
						pageNum: this.current,
						pageSize: 40
					},
					method: "POST",
					success: (res) => {
						if (res.data.code === 200) {
							this.tableList = res.data.data.list;
							this.total = res.data.data.total;
						} else {
							this.$refs.notify.show({
								message: res.data.message,
							});
						}
					},
				});
			},
			getTableList2() {
				let token = uni.getStorageSync("token");
				let userInfo = uni.getStorageSync("userInfo");
				const now = new Date();
				const year = now.getFullYear();
				const month = (now.getMonth() + 1).toString().padStart(2, "0");
				const day = now.getDate();
				const dateBase = year + "-" + month + "-" + day;
				console.log(userInfo);
				uni.request({
					url: "/kl/pad/stitchProductionReport/getNotReportProductDetails",
					header: {
						Authorization: uni.getStorageSync("token"), //自定义请求头信息
					},
					data: {
						...userInfo,
						deptName: userInfo.departmentList[0].deptName,
						deptNo: userInfo.departmentList[0].deptNo,
						sysDate: dateBase,
						lotNo: this.lotNo,
						dateStart1: this.dateStart,
						dateEnd1: this.dateEnd,
					},
					method: "POST",
					success: (res) => {
						console.log(res);
						if (res.data.code === 200) {
							this.tableList2 = res.data.data.list;
						} else {
							this.$refs.notify.show({
								message: res.data.message,
							});
						}
					},
				});
			},
			lookDone(row) {
				this.userInfo = uni.getStorageSync("userInfo");
				uni.navigateTo({
					url: '/pages/reportedProductionProcess/reportedProductionProcess?selectRow=' + JSON.stringify(
							row) +
						'&searchObj=' + JSON.stringify({
							deptName: this.userInfo.departmentList[0].deptName,
							deptNo: this.userInfo.departmentList[0].deptNo
						}) + '&status=2'
				})
			}
		},
		onShow() {
			this.getTableList();
		},
		watch: {
			done(newVal, oldVal) {
				if (newVal === false) {
					this.getTableList2();
				}
				console.log(newVal);
			},
		},
	};
</script>

<style>
	.flex {
		display: flex;
	}

	.search {
		display: flex;
		justify-content: space-between;
		margin: 20rpx 0;
	}

	.content {
		padding: 30rpx;
	}

	.header {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}

	.uni-group {
		display: flex;
		align-items: center;
	}
</style>